package com.cskaoyan.model;

import com.cskaoyan.util.FileUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * 模拟学生数据源
 *
 * @since 14:09
 * @author wuguidong@com.com.cskaoyan.onaliyun.com
 */
public class StudentData {
    public static final Student[] STUDS = new Student[20];
    public static final String[] COLUMNS;

    static {
        // 初始化
        init();
        // 表格列数据
        COLUMNS = new String[]{"学号", "姓名", "性别", "学校", "专业", "年龄", "城市", "手机号", "电子邮箱"};
    }

    // 提供给view层刷新表格时,重新读取文件使用,勿动!
    public static void refreshFileData() {
        init();
    }

    /**
     * 从students.txt文件中将Student对象读出来,然后给STUDS数组中的元素赋值
     * 该txt文件中的一行数据就表示一个Student对象
     * 先读取一行数据,然后将数据中表示的各个属性的取值解析出来,创建相应Student对象
     * 最后将Student对象逐一存放入STUDS数组
     *
     * @author wuguidong@cskaoyan.onaliyun.com
     * @since 16:07
     */
    private static void init() {
        // 获取文件目录路径
        File studentsFile = FileUtils.studentsFile;
        int count = 0;
        try {
            BufferedReader in = new BufferedReader(new FileReader(studentsFile));
            String line;
            // 利用缓冲字符输入流的readLine方法,一次性读一整行字符串数据
            while ((line = in.readLine()) != null) {
                /*
                    String数组中第一个元素是:"stuId=1"
                    第二个元素是:"name=李明"
                    ....
                    接下来继续用split方法用"="分割即可得到对应属性取值
                 */
                String[] studentInfo = line.split(",");
                String id = studentInfo[0].split("=")[1];
                String name = studentInfo[1].split("=")[1];
                String gender = studentInfo[2].split("=")[1];
                /*
                    school和major可能取值是空(长度为0的字符串)
                    所以studentInfo数组中关于school和major的元素可能是:
                    "school=","major="
                    所以要对这两个属性值做判断,不能直接调用split方法
                 */
                String school;
                String[] schoolInfo = studentInfo[3].split("=");
                if (schoolInfo.length == 1) {
                    // 说明school的取值为""
                    school = "";
                } else {
                    school = schoolInfo[1];
                }
                String major;
                String[] majorInfo = studentInfo[4].split("=");
                if (majorInfo.length == 1) {
                    // 说明major的取值为""
                    major = "";
                } else {
                    major = majorInfo[1];
                }
                String age = studentInfo[5].split("=")[1];
                String city = studentInfo[6].split("=")[1];
                String phone = studentInfo[7].split("=")[1];
                String email = studentInfo[8].split("=")[1];
                // 创建Student对象
                Student s = new Student(id, name, gender, school, major, age, city, phone, email);
                STUDS[count] = s;
                count++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
